package org.kisekiproject.evaluator {
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import org.kisekiproject.evaluator.IVariableNamespace;
	import org.kisekiproject.evaluator.events.VariableChangeEvent;

	/**
	 * @author mikael
	 */
	public class VariableStore extends EventDispatcher implements IVariableNamespace {

		private var _dictionary:Dictionary;

		/**
		 * Constructor.
		 */
		public function VariableStore() {
			_dictionary=new Dictionary();
		}

		/**
		 * Get variable.
		 */
		public function getVariable(name : String) : Object {
			if (_dictionary[name]===undefined)
				throw new Error("The variable is not defined: "+name);

			return _dictionary[name];
		}

		/**
		 * Set variable.
		 */
		public function setVariable(variable:String, value:Object):void {
			var old:Object=_dictionary[variable];

			_dictionary[variable]=value;

			if (old!=value)
				dispatchEvent(new VariableChangeEvent(VariableChangeEvent.VARIABLE_CHANGE, variable));
		}
	}
}
